import axios from "axios";
import qs from "qs";
import NProgress from "nprogress";

axios.defaults.baseURL = "http://bookapi.jiaynet.cn:8084/";
axios.defaults.timeout = 10000;
axios.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8;";

axios.interceptors.request.use(
  (config) => {
    var token = window.sessionStorage.getItem("Authorization");
    if (token) {
      config.headers.Authorization = token;
    }
    return config;
  },
  (error) => {
    return Promise.error(error);
  }
);
axios.interceptors.response.use((res) => {
  if (res.data.code === 3001) {
    Message({
      message: "权限不足",
      type: "error",
    });
  }
  return res;
});
//uploadAsome
var uploadAsome = axios.create({
  baseURL: "http://bookapi.jiaynet.cn:8084/",
  timeout: 10000,
  headers: { "Content-Type": "application/x-www-form-urlencoded" },
});
uploadAsome.interceptors.request.use(
  (config) => {
    var token = window.sessionStorage.getItem("Authorization");
    if (token) {
      config.headers.Authorization = token;
    }
    return config;
  },
  (error) => {
    return Promise.error(error);
  }
);
var uploadAsome1 = axios.create({
  baseURL: "http://bookapi.jiaynet.cn:8084/",
  timeout: 10000,
  headers: { "Content-Type": "text/xml;charset=UTF-8/xml" },
});
uploadAsome1.interceptors.request.use(
  (config) => {
    var token = window.sessionStorage.getItem("Authorization");
    if (token) {
      config.headers.Authorization = token;
    }
    return config;
  },
  (error) => {
    return Promise.error(error);
  }
);

//uploadRequest
var uploadRequest = axios.create({
  baseURL: "/api",
  timeout: 10000,
  headers: { "Content-Type": "multipart/form-data" },
});
uploadRequest.interceptors.request.use(
  (config) => {
    var token = window.sessionStorage.getItem("Authorization");
    if (token) {
      config.headers.Authorization = token;
    }
    return config;
  },
  (error) => {
    return Promise.error(error);
  }
);
var uploadRequest1 = axios.create({
  baseURL: "/api",
  timeout: 10000,
  headers: { "Content-Type": "application/x-www-form-urlencoded" },
});
uploadRequest1.interceptors.request.use(
  (config) => {
    var token = window.sessionStorage.getItem("Authorization");
    if (token) {
      config.headers.Authorization = token;
    }
    return config;
  },
  (error) => {
    return Promise.error(error);
  }
);
var uploadRequest2 = axios.create({
  baseURL: "/api",
  timeout: 10000,
  headers: { "Content-Type": "application/json" },
});
uploadRequest2.interceptors.request.use(
  (config) => {
    var token = window.sessionStorage.getItem("Authorization");
    if (token) {
      config.headers.Authorization = token;
    }
    return config;
  },
  (error) => {
    return Promise.error(error);
  }
);

//
var http = {
  get(url, params) {
    return new Promise((resolve, reject) => {
      NProgress.start();
      axios
        .get(url, {
          params: params,
        })
        .then((res) => {
          NProgress.done();
          // if (res.status === 200 && url === "/login") {
          //   window.sessionStorage.setItem("Authorization", res.headers.token);
          // }

          resolve(res.data);
        })
        .catch((err) => {
          NProgress.done();
          reject(err.data);
        });
    });
  },
  post(url, params, message = false) {
    return new Promise((resolve, reject) => {
      NProgress.start();
      axios
        .post(url, JSON.stringify(params))
        .then((res) => {
          NProgress.done();
          if (message) {
            Message({
              message: res.data.message,
              type: res.data.code === 200 ? "success" : "error",
            });
          }
          resolve(res.data);
        })
        .catch((err) => {
          NProgress.done();
          reject(err.data);
        });
    });
  },
  asomeGet(url, params) {
    return new Promise((resolve, reject) => {
      NProgress.start();
      uploadAsome
        .get(url, {
          params: params,
        })
        .then((res) => {
          console;
          NProgress.done();
          if (res.status === 200 && url === "/login") {
            window.sessionStorage.setItem("Authorization", res.headers.token);
          }

          resolve(res.data);
        })
        .catch((err) => {
          NProgress.done();
          reject(err.data);
        });
    });
  },
  asomePost(url, params, message = false) {
    return new Promise((resolve, reject) => {
      NProgress.start();
      uploadAsome
        .post(url, qs.stringify(params))
        .then((res) => {
          NProgress.done();
          if (message) {
            Message({
              message: res.data.message,
              type: res.data.code === 200 ? "success" : "error",
            });
          }
          resolve(res.data);
        })
        .catch((err) => {
          NProgress.done();
          reject(err.data);
        });
    });
  },
  formPost(url, file) {
    return new Promise((resolve, reject) => {
      NProgress.start();
      uploadRequest1
        .post(url, file)
        .then((res) => {
          NProgress.done();
          1;
          resolve(res.data);
        })
        .catch((err) => {
          NProgress.done();
          reject(err.data);
        });
    });
  },
  Post(url, file) {
    return new Promise((resolve, reject) => {
      NProgress.start();
      uploadRequest2
        .post(url, file)
        .then((res) => {
          NProgress.done();
          1;
          resolve(res.data);
        })
        .catch((err) => {
          NProgress.done();
          reject(err.data);
        });
    });
  },
  upload(url, file) {
    return new Promise((resolve, reject) => {
      NProgress.start();
      uploadRequest
        .post(url, file)
        .then((res) => {
          NProgress.done();
          resolve(res.data);
        })
        .catch((err) => {
          NProgress.done();
          reject(err.data);
        });
    });
  },
  download(url) {
    let iframe = document.createElement("iframe");
    iframe.style.display = "none";
    iframe.src = url;
    iframe.onload = function() {
      document.body.removeChild(iframe);
    };
    document.body.appendChild(iframe);
  },
  install(Vue) {
    Vue.prototype.http = this;
    delete this.install;
  },
};
export default http;
